package com.syxp.pda.service.base.impl;

import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.dao.BaseDao;
import com.syxp.pda.core.service.impl.BaseServiceImpl;
import com.syxp.pda.core.util.ArraysBuilder;
import com.syxp.pda.dao.base.UserDao;
import com.syxp.pda.dao.system.RoleDao;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.base.UserService;

@Transactional
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{

	private UserDao userDao;
	private RoleDao roleDao;
	
	public UserServiceImpl(BaseDao<User> baseDao) {
		super(baseDao) ;
		this.userDao = (UserDao) baseDao;
	}
	
	@Override
	public User getUserByAccount(String account) {
		User user = this.userDao.queryUserByAccount(account);
		if(user!=null) {
			Set<Role> roleSet = user.getRoles();
			user.setRoles(ArraysBuilder.buildSet(roleSet));
		}
		return user;
	}

	@Override
	public void setRole(int userId, int roleId) {
		User user = this.userDao.get(userId);
		Set<Role> roleSet = user.getRoles();
		Role role = this.roleDao.get(roleId);
		roleSet.add(role);
		this.userDao.update(user);
	}

	@Override
	public void modifyRole(int oldUserId, int oldRoleId, int userId, int roleId) {
		if(oldUserId==userId&&oldRoleId==roleId) {
			return ;
		}else if(oldUserId==userId) {
			User user = this.userDao.get(userId);
			Set<Role> roleSet = user.getRoles();
			Role oldRole = this.roleDao.get(oldRoleId);
			Role role = this.roleDao.get(roleId);
			roleSet.remove(oldRole);
			this.userDao.save(user);
			roleSet.add(role);
			this.userDao.update(user);
		} else {
			User oldUser = this.userDao.get(oldUserId);
			Set<Role> oldRoleSet = oldUser.getRoles();
			oldRoleSet.clear();
			this.userDao.update(oldUser);
			
			User user = this.userDao.get(userId);
			Set<Role> roleSet = user.getRoles();
			Role role = this.roleDao.get(roleId);
			roleSet.add(role);
			this.userDao.update(user);
		}
	}

	@Override
	public void removeRole(int userId, int roleId) {
		User user = this.userDao.get(userId);
		Set<Role> roleSet = user.getRoles();
		Role role = this.roleDao.get(roleId);
		roleSet.remove(role);
		this.userDao.update(user);
		
	}

	/**
	 * @return the roleDao
	 */
	public RoleDao getRoleDao() {
		return roleDao;
	}

	/**
	 * @param roleDao the roleDao to set
	 */
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	@Override
	public User updateService(User param) {
		User user = super.loadService(param.getUserID()) ;
		user.setDepartment(param.getDepartment());
		user.setOperatorCode(param.getOperatorCode()) ;
		user.setProfess(param.getProfess()) ;
		user.setRemarks(param.getRemarks()) ;
		user.setStation(param.getStation()) ;
		user.setUserAccount(param.getUserAccount()) ;
		user.setUserName(param.getUserName()) ;
		user.setPassWord(param.getPassWord());
		return super.updateService(user);
	}
}
